package com.fragment;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Xml;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.adapter.HorgradeAdapter;
import com.adapter.HorgradeAdapter2;
import com.adapter.MainRecyclerViewAdapter;
import com.adapter.OneTypeAdapter;
import com.bean.HomeListBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.myapp.MyApp;
import com.myview.CustomProgressDialog;
import com.myview.GridViewForScrollView;
import com.myview.HorizontalListView;
import com.myview.PullLoadMoreRecyclerView;
import com.myview.RatioImageView;
import com.myview.XCFlowLayout;
import com.util.DensityUtil;
import com.util.GlideUtils;
import com.util.InfoEventMessage;
import com.util.MyLog;
import com.util.RequestManager;
import com.util.ToastUtils;
import com.util.UIUtil;
import com.xiaochun.hxhj.AnswerQueActivity;
import com.xiaochun.hxhj.BuyBookActivity;
import com.xiaochun.hxhj.CourseListActivity;
import com.xiaochun.hxhj.CourseListActivity2;
import com.xiaochun.hxhj.FreeReadListActivity;
import com.xiaochun.hxhj.FreeSpeedActivity;
import com.xiaochun.hxhj.LoginActivity;
import com.xiaochun.hxhj.MyCourseActivity;
import com.xiaochun.hxhj.MyCourseListActivity;
import com.xiaochun.hxhj.R;
import com.xiaochun.hxhj.TestListActivity;
import com.xiaochun.hxhj.VideoListActivity;
import com.xiaochun.hxhj.WebviewActivity;
import com.xiaochun.hxhj.ZuowenListActivity;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;


public class HomeFragment extends Fragment {


    private Context mcontext;
    private MyApp myapp;
    private View view;
    private PullLoadMoreRecyclerView homedataRecycleView;
    private LinearLayoutManager layoutManager;
    private View bannerView;
    private MainRecyclerViewAdapter adapter;
    private View bottomView;
    private Banner banner;
//    private Banner banner2;
    private Banner banner3;
    private LinearLayout loadLlay;
    private View notMoreShopView;
    private View loadMoreShopView;
    private CustomProgressDialog customProgressDialog;
    public List<String> bannerlist = new ArrayList<>();
    public List<String> bannerlist2 = new ArrayList<>();
    public List<String> bannerlist3 = new ArrayList<>();
    /*public List<MyCourseBean> myCourselist = new ArrayList<>();
    public List<MentalBean> mmentallist = new ArrayList<>();
    public List<FreeListBean> freeListlist = new ArrayList<>();*/
    private int width;
    private LinearLayout ll_mycourse;
    private LinearLayout ll_courseone;
    private ImageView img_courseone;
    private TextView tv_courseone;
    private LinearLayout ll_coursetwo;
    private ImageView img_coursetwo;
    private TextView tv_coursetwo;
    private ImageView img_buycourse;
    private LinearLayout ll_more_course;
    private View recommendView;
    private HorizontalListView horlistview;
    private LinearLayout ll_recommend;
    private HorgradeAdapter horgradeAdapter;
    private View freeView;
    private LinearLayout ll_freespeedone;
    private ImageView img_freespeedone;
    private TextView tv_freespeedone;
    private TextView tv_authorone;
    private LinearLayout ll_freespeedtwo;
    private ImageView img_freespeedtwo;
    private TextView tv_freespeedtwo;
    private TextView tv_authortwo;
    private LinearLayout ll_freespeedthree;
    private ImageView img_freespeedthree;
    private TextView tv_freespeedthree;
    private TextView tv_authorthree;
    private HorizontalListView freehorlistview;
    private HorgradeAdapter freehorgradeAdapter;
    private LinearLayout ll_train;
    private TextView tv_titletest;
    private List<HomeListBean.DataBean.RecommendBean> recommendlist = new ArrayList<>();
    private List<HomeListBean.DataBean.MyCourseBean> myCourselist = new ArrayList<>();
    private List<HomeListBean.DataBean.MentalBean> mmentallist = new ArrayList<>();
    private List<HomeListBean.DataBean.IosMentalBean> iosmmentallist = new ArrayList<>();
    private List<HomeListBean.DataBean.FreeListBean> freeListlist = new ArrayList<>();


    private HorgradeAdapter2 horgradeAdapter2;
    private SharedPreferences sharedPreferences;
    private String token;
    private TextView tv_freeone_flag;
    private TextView tv_freetwo_flag;
    private TextView tv_freethree_flag;
    private TextView tv_free_read_more;
    private TextView tv_recommend_more;
    private TextView tv_xunlian_more;
    private PopupWindow mPopupWindowtime;
    private TextView tv_mycourse_more;
    private XCFlowLayout xcyingxiangone;
    private XCFlowLayout xcyingxiangtwo;
    private LayoutInflater mInflater;
    private String first_title;
    private TextView tv_onemodletitle;
    private GridViewForScrollView gridview_onetype;
    private OneTypeAdapter onetypeadapter;
    private List<HomeListBean.DataBean.BannerBean> bannerlistdata = new ArrayList<>();
    private ImageView img_banner3;
    private ImageView img_banner;
//    private ImageView img_banner2;
    private String is_has_recommend;
    private LinearLayout ll_recommendshow;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e("Activity:", this.getClass().getName().toString());
        view = inflater.inflate(R.layout.fragment_home, container, false);
        mcontext = getActivity();
        myapp = (MyApp) mcontext.getApplicationContext();
        EventBus.getDefault().register(this);
        ButterKnife.bind(getActivity());
        customProgressDialog = new CustomProgressDialog(mcontext, "", R.style.CommProgressDialog);
        WindowManager.LayoutParams params = customProgressDialog.getWindow().getAttributes();
        params.alpha = 0.7f;//取值为0-1之间  0是全透明，1是不透明
        params.dimAmount = 0f;   //取值为0-1之间  1是全黑
        customProgressDialog.getWindow().setAttributes(params);
        customProgressDialog.show();
        sharedPreferences = mcontext.getSharedPreferences("userinfo", Context.MODE_MULTI_PROCESS);
        token = sharedPreferences.getString("token", "");
        getScreenDen();
        initView();
        initTopView();
        initLin();
        return view;

    }


    private void initView() {
        horgradeAdapter = new HorgradeAdapter(mcontext, mmentallist);
        horgradeAdapter2 = new HorgradeAdapter2(mcontext, recommendlist);
//        freehorgradeAdapter = new HorgradeAdapter(mcontext,mmentallist);
        tv_titletest = (TextView) view.findViewById(R.id.tv_titletest);
        homedataRecycleView = view.findViewById(R.id.home_data);
        adapter = new MainRecyclerViewAdapter(recommendlist, mcontext, myapp, width);
        setHeader(homedataRecycleView);
        homedataRecycleView.setAdapter(adapter);
        homedataRecycleView.setLinearLayout();
        homedataRecycleView.setRefreshing(true);
        layoutManager = (LinearLayoutManager) homedataRecycleView.getLayoutManager();
//        GridLayoutManager gridLayoutManager = homedataRecycleView.getGridLayoutManager();
        homedataRecycleView.cancleAnimal();
        loadMoreShopView = View.inflate(mcontext, R.layout.item_moredata, null);
        notMoreShopView = View.inflate(mcontext, R.layout.item_nodata, null);

        mInflater = LayoutInflater.from(mcontext);
    }

    @Subscribe
    public void onEventMainThread(InfoEventMessage event) {
        String message = event.getMessage();
        Log.e("GTPCmessage--new--order", message);
        if ("轮播图时间".equals(message)){
            String banner_time = myapp.getBanner_time();
            if ("".equals(banner_time)){
                banner_time = "3000";
            }
            banner.setDelayTime(Integer.parseInt(banner_time));
        }

    }

    /**
     * 设置头部布局
     *
     * @param view
     */
    private void setHeader(PullLoadMoreRecyclerView view) {
        //轮播图
        bannerView = LayoutInflater.from(mcontext).inflate(R.layout.item_home_banner, view, false);
        adapter.setBannerView(bannerView);
        //为您推荐
        recommendView = LayoutInflater.from(mcontext).inflate(R.layout.item_home_recommend, view, false);
        adapter.setRecommendView(recommendView);
        //免费速读
        freeView = LayoutInflater.from(mcontext).inflate(R.layout.item_home_free, view, false);
        adapter.setFreeView(freeView);

        //底部加载更多
        bottomView = LayoutInflater.from(mcontext).inflate(R.layout.main_bottom, view, false);
        adapter.setBottomView(bottomView);
    }

    /**
     * 初始化头部控件
     */
    private void initTopView() {
        //轮播图
        banner = (Banner) bannerView.findViewById(R.id.banner);
        img_banner = (ImageView) bannerView.findViewById(R.id.img_banner);

//        banner2 = (Banner) bannerView.findViewById(R.id.banner2);
//        img_banner2 = (ImageView) bannerView.findViewById(R.id.img_banner2);
        ll_mycourse = (LinearLayout) bannerView.findViewById(R.id.ll_mycourse);
        ll_courseone = (LinearLayout) bannerView.findViewById(R.id.ll_courseone);
        img_courseone = (ImageView) bannerView.findViewById(R.id.img_courseone);
        tv_courseone = (TextView) bannerView.findViewById(R.id.tv_courseone);
        ll_coursetwo = (LinearLayout) bannerView.findViewById(R.id.ll_coursetwo);
        img_coursetwo = (ImageView) bannerView.findViewById(R.id.img_coursetwo);
        tv_coursetwo = (TextView) bannerView.findViewById(R.id.tv_coursetwo);
        img_buycourse = (ImageView) bannerView.findViewById(R.id.img_buycourse);
        ll_more_course = (LinearLayout) bannerView.findViewById(R.id.ll_more_course);
        tv_mycourse_more = (TextView) bannerView.findViewById(R.id.tv_mycourse_more);
        //75dp
        int i = width - DensityUtil.dip2px(mcontext, 75);
        int itemwidth = i / 3;

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ll_courseone.getLayoutParams();
        layoutParams.width = itemwidth;
        ll_courseone.setLayoutParams(layoutParams);
        LinearLayout.LayoutParams layoutParams1 = (LinearLayout.LayoutParams) ll_coursetwo.getLayoutParams();
        layoutParams1.width = itemwidth;
        ll_coursetwo.setLayoutParams(layoutParams1);
        LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) img_buycourse.getLayoutParams();
        layoutParams2.width = itemwidth;
        img_buycourse.setLayoutParams(layoutParams2);

        //为您推荐
        ll_recommendshow = freeView.findViewById(R.id.ll_recommendshow);
        horlistview = freeView.findViewById(R.id.horlistview);
        horlistview.setAdapter(horgradeAdapter2);
        ll_recommend = freeView.findViewById(R.id.ll_recommend);
        //免费速读训练
        ll_freespeedone = freeView.findViewById(R.id.ll_freespeedone);
        img_freespeedone = freeView.findViewById(R.id.img_freespeedone);
        tv_freespeedone = freeView.findViewById(R.id.tv_freespeedone);
        tv_authorone = freeView.findViewById(R.id.tv_authorone);
        tv_freeone_flag = freeView.findViewById(R.id.tv_freeone_flag);
        ll_freespeedtwo = freeView.findViewById(R.id.ll_freespeedtwo);
        img_freespeedtwo = freeView.findViewById(R.id.img_freespeedtwo);
        tv_freespeedtwo = freeView.findViewById(R.id.tv_freespeedtwo);
        tv_authortwo = freeView.findViewById(R.id.tv_authortwo);
        tv_freetwo_flag = freeView.findViewById(R.id.tv_freetwo_flag);
        ll_freespeedthree = freeView.findViewById(R.id.ll_freespeedthree);
        img_freespeedthree = freeView.findViewById(R.id.img_freespeedthree);
        tv_freespeedthree = freeView.findViewById(R.id.tv_freespeedthree);
        tv_authorthree = freeView.findViewById(R.id.tv_authorthree);
        tv_freethree_flag = freeView.findViewById(R.id.tv_freethree_flag);
        tv_free_read_more = freeView.findViewById(R.id.tv_free_read_more);
        tv_recommend_more = freeView.findViewById(R.id.tv_recommend_more);
        freehorlistview = recommendView.findViewById(R.id.freehorlistview);
        tv_xunlian_more = recommendView.findViewById(R.id.tv_xunlian_more);
        ll_train = recommendView.findViewById(R.id.ll_train);
        banner3 = (Banner) recommendView.findViewById(R.id.banner3);
        img_banner3 = (ImageView) recommendView.findViewById(R.id.img_banner3);
        //影像速记一级分类第三版
        gridview_onetype = (GridViewForScrollView) recommendView.findViewById(R.id.gridview_onetype);
        onetypeadapter = new OneTypeAdapter(mcontext, iosmmentallist);
        gridview_onetype.setAdapter(onetypeadapter);
        //影像速记一级分类第二版
        xcyingxiangone = recommendView.findViewById(R.id.xcyingxiangone);
        //影像速记二级分类
        xcyingxiangtwo = recommendView.findViewById(R.id.xcyingxiangtwo);
        tv_onemodletitle = recommendView.findViewById(R.id.tv_onemodletitle);

        freehorlistview.setAdapter(horgradeAdapter);
        //加载更多
        loadLlay = (LinearLayout) bottomView.findViewById(R.id.llay_load);
        int iq = width - DensityUtil.dip2px(mcontext, 53);
        int itemwidth2 = iq / 3;
        LinearLayout.LayoutParams layoutParam3 = (LinearLayout.LayoutParams) ll_freespeedone.getLayoutParams();
        layoutParam3.width = itemwidth2;
        ll_freespeedone.setLayoutParams(layoutParam3);
        LinearLayout.LayoutParams layoutParams4 = (LinearLayout.LayoutParams) ll_freespeedtwo.getLayoutParams();
        layoutParams4.width = itemwidth2;
        ll_freespeedtwo.setLayoutParams(layoutParams4);
        LinearLayout.LayoutParams layoutParams5 = (LinearLayout.LayoutParams) ll_freespeedthree.getLayoutParams();
        layoutParams5.width = itemwidth2;
        ll_freespeedthree.setLayoutParams(layoutParams5);
    }

    private void initLin() {
        gridview_onetype.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                onetypeadapter.setCheckedItem(i);
                setflagtwo(i);
            }
        });

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

                String jump_type = bannerlistdata.get(position).getJump_type();
                if ("1".equals(jump_type)){
                    ToastUtils.show("敬请期待");
                }else{
                    int id = bannerlistdata.get(position).getId();
                    String title = bannerlistdata.get(position).getTitle();
                    Intent intent = new Intent(mcontext, VideoListActivity.class);
                    intent.putExtra("bannerid", id + "");
                    intent.putExtra("title", title + "");
                    startActivity(intent);
                }


            }
        });
        /*banner2.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                ToastUtils.show("敬请期待");
            }
        });*/
        banner3.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(mcontext, TestListActivity.class);
                startActivity(intent);
            }
        });

        tv_titletest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(mcontext, TestActivity.class);
//                startActivity(intent);
            }
        });

        ll_more_course.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                token = sharedPreferences.getString("token", "");
                if ("".equals(token)) {
                    Intent intent = new Intent(mcontext, LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(mcontext, MyCourseListActivity.class);
                    startActivity(intent);
                }
            }
        });
        img_buycourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                token = sharedPreferences.getString("token", "");
                if ("".equals(token)) {
                    Intent intent = new Intent(mcontext, LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(mcontext, MyCourseListActivity.class);
                    startActivity(intent);
                }

//                Intent intent = new Intent(mcontext, CourseListActivity.class);
//                intent.putExtra("titlename", "为您推荐");
//                startActivity(intent);
            }
        });
        tv_free_read_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popbysettime();

            }
        });
        tv_recommend_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mcontext, CourseListActivity.class);
                intent.putExtra("titlename", "为您推荐");
                startActivity(intent);
            }
        });
        tv_xunlian_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mcontext, CourseListActivity2.class);
                intent.putExtra("titlename", "影像速记");
                startActivity(intent);
            }
        });
        ll_courseone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                token = sharedPreferences.getString("token", "");
                if ("".equals(token)) {
                    Intent intent = new Intent(mcontext, LoginActivity.class);
                    startActivity(intent);
                } else {
                    int size = myCourselist.size();
                    if (size > 0) {
//                        String id = myCourselist.get(0).getId();
                        int course_id = myCourselist.get(0).getCourse_id();
                        Intent intent = new Intent(mcontext, MyCourseActivity.class);
                        intent.putExtra("id", course_id + "");
                        startActivity(intent);
                    }
                }


            }
        });
        ll_coursetwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                token = sharedPreferences.getString("token", "");
                if ("".equals(token)) {
                    Intent intent = new Intent(mcontext, LoginActivity.class);
                    startActivity(intent);
                } else {
                    int size = myCourselist.size();
                    if (size > 0) {
//                        String id = myCourselist.get(1).getId();
                        int course_id = myCourselist.get(1).getCourse_id();
                        Intent intent = new Intent(mcontext, MyCourseActivity.class);
                        intent.putExtra("id", course_id + "");
                        startActivity(intent);
                    }
                }
            }
        });
        horlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                horgradeAdapter2.setCheckedItem(i);
                setRecommendData(i);
            }
        });
        freehorlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                horgradeAdapter.setCheckedItem(i);
                setTrainData(i);
            }
        });
        adapter.setOnItemClickListener(new MainRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });
        homedataRecycleView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                getData();

            }

            @Override
            public void onLoadMore() {
                homedataRecycleView.setPullLoadMoreCompleted();
            }

            @Override
            public void onScrollChanged(int heightTotal) {

            }

            @Override
            public void onScrollState(int type) {

            }
        });
        ll_freespeedone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = freeListlist.get(0).getId();
                String if_pay = freeListlist.get(0).getIf_pay();

                if ("1".equals(if_pay)) {
                    //需要支付
                    Intent intent = new Intent(mcontext, BuyBookActivity.class);
                    intent.putExtra("id", id + "");
                    startActivity(intent);
                } else if ("0".equals(if_pay)) {
                    //不需要付费
                    if ("".equals(token)) {
                        Intent intent = new Intent(mcontext, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(mcontext, FreeSpeedActivity.class);
                        intent.putExtra("id", id + "");
                        startActivity(intent);
                    }
                }


            }
        });
        ll_freespeedtwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = freeListlist.get(1).getId();
                String if_pay = freeListlist.get(1).getIf_pay();
                if ("1".equals(if_pay)) {
                    //需要支付
                    Intent intent = new Intent(mcontext, BuyBookActivity.class);
                    intent.putExtra("id", id + "");
                    startActivity(intent);
                } else if ("0".equals(if_pay)) {
                    //不需要付费
                    if ("".equals(token)) {
                        Intent intent = new Intent(mcontext, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(mcontext, FreeSpeedActivity.class);
                        intent.putExtra("id", id + "");
                        startActivity(intent);
                    }
                }


            }
        });
        ll_freespeedthree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = freeListlist.get(2).getId();
                String if_pay = freeListlist.get(2).getIf_pay();
                if ("1".equals(if_pay)) {
                    //需要支付
                    Intent intent = new Intent(mcontext, BuyBookActivity.class);
                    intent.putExtra("id", id + "");
                    startActivity(intent);
                } else if ("0".equals(if_pay)) {
                    //不需要付费
                    if ("".equals(token)) {
                        Intent intent = new Intent(mcontext, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(mcontext, FreeSpeedActivity.class);
                        intent.putExtra("id", id + "");
                        startActivity(intent);
                    }
                }


            }
        });
    }

    /**
     * 弹出搜索框
     */
    private void popbysettime() {

        // 1~100   20


        LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
        View popupWindow = layoutInflater.inflate(R.layout.layout_zworsudu, null);

        LinearLayout ll_zuowen = popupWindow.findViewById(R.id.ll_zuowen);
        LinearLayout ll_mingzhu = popupWindow.findViewById(R.id.ll_mingzhu);
        ll_mingzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindowtime.dismiss();
                Intent intent = new Intent(mcontext, FreeReadListActivity.class);
                startActivity(intent);
            }
        });
        ll_zuowen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindowtime.dismiss();
                Intent intent = new Intent(mcontext, ZuowenListActivity.class);
                startActivity(intent);
            }
        });

        mPopupWindowtime = new PopupWindow(popupWindow, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        int mPopupWindowWidth = mPopupWindowtime.getWidth();
        int mPopupWindowHeight = mPopupWindowtime.getHeight();
        // 需要设置一下此参数，点击外边可消失
        mPopupWindowtime.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindowtime.setOutsideTouchable(true);
        mPopupWindowtime.setTouchable(true);
        mPopupWindowtime.setFocusable(true); // pop 显示时， 不让外部 view 响应点击事件
        //防止被底部虚拟键挡住
        mPopupWindowtime.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        if (Build.VERSION.SDK_INT >= 24) { // Android 7.x中,PopupWindow高度为match_parent时,会出现兼容性问题,需要处理兼容性
            int Screenheightth = getActivity().getWindowManager().getDefaultDisplay().getHeight();
            mPopupWindowtime.showAtLocation(tv_authorone,
                    Gravity.BOTTOM,
                    0,
                    Screenheightth / 2);
//            Log.e("底部导航栏高度", utils.getNavigationBarHeight(context) + "    gaodu");

        } else {
            int Screenheightth = getActivity().getWindowManager().getDefaultDisplay().getHeight();
            mPopupWindowtime.showAtLocation(tv_authorone,
                    Gravity.BOTTOM,
                    0,
                    0);
        }
        mPopupWindowtime.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });

    }


    private void getData() {
        token = sharedPreferences.getString("token", "");
        String url = myapp.getWebConfig() + "/api/banner/Content";
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", token);
        MyLog.e("首页token", token + "   ");
//        paramsMap.put("is_material", apptype);
        RequestManager.getInstance(mcontext).requestAsyn(url, RequestManager.TYPE_POST_FORM, paramsMap, new RequestManager.ReqCallBack() {
            @Override
            public void onReqSuccess(Object result) {
                MyLog.e("code--result", result.toString());
                Message msg = new Message();
                try {
                    JSONObject jsonObject = new JSONObject(result.toString());
                    if (jsonObject.getString("code").equals("0")) {
                        msg.obj = jsonObject.getString("msg");
                        msg.arg1 = 0;
                        handler.sendMessage(msg);
                        return;
                    }
                    JSONObject data = jsonObject.getJSONObject("data");
                    first_title = data.getString("first_title");
                    is_has_recommend = data.getString("is_has_recommend");
                    JSONArray banner = data.getJSONArray("banner");
                    int bannerlength = banner.length();
                    bannerlist.clear();
                    bannerlistdata.clear();
                    for (int i = 0; i < bannerlength; i++) {
                        String image = banner.getJSONObject(i).getString("image");
                        bannerlist.add(image);
                    }
                    bannerlistdata = new Gson().fromJson(banner.toString(), new TypeToken<List<HomeListBean.DataBean.BannerBean>>() {
                    }.getType());
                    JSONArray banner2 = data.getJSONArray("banner2");
                    int bannerlength2 = banner2.length();
                    bannerlist2.clear();
                    for (int i = 0; i < bannerlength2; i++) {
                        String image = banner2.getJSONObject(i).getString("image");
                        bannerlist2.add(image);
                    }
                    JSONArray banner3 = data.getJSONArray("banner3");
                    int bannerlength3 = banner3.length();
                    bannerlist3.clear();
                    for (int i = 0; i < bannerlength3; i++) {
                        String image = banner3.getJSONObject(i).getString("image");
                        bannerlist3.add(image);
                    }

                    myCourselist.clear();
                    JSONArray myCourse = data.getJSONArray("myCourse");
                    myCourselist = new Gson().fromJson(myCourse.toString(), new TypeToken<List<HomeListBean.DataBean.MyCourseBean>>() {
                    }.getType());
                    mmentallist.clear();
                    JSONArray mental = data.getJSONArray("mental");
                    mmentallist = new Gson().fromJson(mental.toString(), new TypeToken<List<HomeListBean.DataBean.MentalBean>>() {
                    }.getType());
                    iosmmentallist.clear();
                    JSONArray ios_mental = data.getJSONArray("ios_mental");
                    iosmmentallist = new Gson().fromJson(ios_mental.toString(), new TypeToken<List<HomeListBean.DataBean.IosMentalBean>>() {
                    }.getType());
                    freeListlist.clear();
                    JSONArray freeList = data.getJSONArray("freeList");
                    freeListlist = new Gson().fromJson(freeList.toString(), new TypeToken<List<HomeListBean.DataBean.FreeListBean>>() {
                    }.getType());

                    recommendlist.clear();
                    JSONArray recommend = data.getJSONArray("recommend");
                    recommendlist = new Gson().fromJson(recommend.toString(), new TypeToken<List<HomeListBean.DataBean.RecommendBean>>() {
                    }.getType());

                    msg.arg1 = 2;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    msg.arg1 = 1;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                if (errorMsg.equals("未登录")) {
                    Message msg = new Message();
                    msg.arg1 = 11;
                    handler.sendMessage(msg);
                } else {
                    Message msg = new Message();
                    msg.arg1 = 12;
                    handler.sendMessage(msg);
                }
            }
        });
    }


    public Handler handler = new Handler() {

        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.arg1) {
                case 0:
                    if (customProgressDialog != null) {
                        if (customProgressDialog.isShowing()) {
                            customProgressDialog.dismiss();
                        }
                    }
                    homedataRecycleView.setPullLoadMoreCompleted();
                    String obj = (String) msg.obj;
                    ToastUtils.show(obj);
                    break;
                case 1:
                    if (customProgressDialog != null) {
                        if (customProgressDialog.isShowing()) {
                            customProgressDialog.dismiss();
                        }
                    }
                    homedataRecycleView.setPullLoadMoreCompleted();
                    break;
                case 2:
                    if (customProgressDialog != null) {
                        if (customProgressDialog.isShowing()) {
                            customProgressDialog.dismiss();
                        }
                    }
                    tv_onemodletitle.setText(first_title);
                    if ("1".equals(is_has_recommend)){
                        //1显示
                        ll_recommendshow.setVisibility(View.VISIBLE);
                    }else if ("0".equals(is_has_recommend)){
                        //0隐藏
                        ll_recommendshow.setVisibility(View.GONE);
                    }

                    if (myCourselist.size() > 0) {
                        if (myCourselist.size() == 1) {
                            ll_courseone.setVisibility(View.VISIBLE);
                            ll_coursetwo.setVisibility(View.GONE);
                            tv_courseone.setText(myCourselist.get(0).getName());
                            String image = myCourselist.get(0).getImage();
                            if (!"".equals(image)) {
                                GlideUtils.loadRoundCircleImage(getActivity().getApplicationContext(), image, img_courseone);
                            }
                        } else if (myCourselist.size() == 2) {
                            ll_courseone.setVisibility(View.VISIBLE);
                            ll_coursetwo.setVisibility(View.VISIBLE);
                            tv_courseone.setText(myCourselist.get(0).getName());
                            String image = myCourselist.get(0).getImage();
                            if (!"".equals(image)) {
                                GlideUtils.loadRoundCircleImage(getActivity().getApplicationContext(), image, img_courseone);
                            }
                            tv_coursetwo.setText(myCourselist.get(1).getName());
                            String imagetwo = myCourselist.get(1).getImage();
                            if (!"".equals(imagetwo)) {
                                GlideUtils.loadRoundCircleImage(getActivity().getApplicationContext(), imagetwo, img_coursetwo);
                            }
                        }
                    } else {
                        ll_courseone.setVisibility(View.GONE);
                        ll_coursetwo.setVisibility(View.GONE);
                    }
                    horgradeAdapter.setData(mmentallist);
                    horgradeAdapter.setCheckedItem(0);
                    horgradeAdapter2.setData(recommendlist);
                    horgradeAdapter2.setCheckedItem(0);
                    setRecommendData(0);
                    setTrainData(0);
                    setFreerepeed();
                    homedataRecycleView.setPullLoadMoreCompleted();
                    onetypeadapter.setData(iosmmentallist);

                    xcyingxiangone.removeAllViews();
                    xcyingxiangtwo.removeAllViews();
                    int sizeone = iosmmentallist.size();
                    if (sizeone > 0) {
                        setflagtwo(0);
                    }
                    img_banner.setVisibility(View.GONE);
                    banner.setVisibility(View.VISIBLE);
                    banner.setImageLoader(new GlideImageMidlllunboLoader());
                    banner.setImages(bannerlist);
                    banner.start();
//                    img_banner2.setVisibility(View.GONE);
//                    banner2.setVisibility(View.VISIBLE);
//                    banner2.setImageLoader(new GlideImageMidlllunboLoader());
//                    banner2.setImages(bannerlist2);
//                    banner2.start();
                    img_banner3.setVisibility(View.GONE);
                    banner3.setVisibility(View.VISIBLE);
                    banner3.setImageLoader(new GlideImageMidlllunboLoader());
                    banner3.setImages(bannerlist3);
                    banner3.start();


                    /*if (bannerlist.size() == 1){
                        img_banner.setVisibility(View.VISIBLE);
                        banner.setVisibility(View.GONE);
                        //计算图片左右间距之和
                        int padding3 = 15;
                        int spacePx3 = (int) (UIUtil.dp2px(mcontext, padding3) * 2);
                        //计算图片宽度
                        int imageWidth3 = UIUtil.getScreenWidth(mcontext) - spacePx3;
                        //计算宽高比，注意数字后面要加上f表示浮点型数字
                        float scale3 = 23f / 8f;
                        //根据图片宽度和比例计算图片高度
                        int imageHeight3 = (int) (imageWidth3 / scale3);
                        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams( imageWidth3,imageHeight3);
                        //设置左右边距
                        params3.leftMargin = (int) UIUtil.dp2px(mcontext, padding3);
                        params3.rightMargin = (int) UIUtil.dp2px(mcontext, padding3);
                        img_banner.setLayoutParams(params3);
                        GlideUtils.loadImage(mcontext,bannerlist.get(0),img_banner);

                    }else{
                        img_banner.setVisibility(View.GONE);
                        banner.setVisibility(View.VISIBLE);
                        banner.setImageLoader(new GlideImageMidlllunboLoader());
                        banner.setImages(bannerlist);
                        banner.start();
                    }
                    if (bannerlist2.size() == 1){
                        img_banner2.setVisibility(View.VISIBLE);
                        banner2.setVisibility(View.GONE);
                        //计算图片左右间距之和
                        int padding2 = 15;
                        int spacePx2 = (int) (UIUtil.dp2px(mcontext, padding2) * 2);
                        //计算图片宽度
                        int imageWidth2 = UIUtil.getScreenWidth(mcontext) - spacePx2;
                        //计算宽高比，注意数字后面要加上f表示浮点型数字
                        float scale2 = 23f / 10f;
                        //根据图片宽度和比例计算图片高度
                        int imageHeight2 = (int) (imageWidth2 / scale2);
                        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( imageWidth2,imageHeight2);
                        //设置左右边距
                        params2.leftMargin = (int) UIUtil.dp2px(mcontext, padding2);
                        params2.rightMargin = (int) UIUtil.dp2px(mcontext, padding2);
                        img_banner2.setLayoutParams(params2);
                        GlideUtils.loadImage(mcontext,bannerlist2.get(0),img_banner2);
                    }else {
                        img_banner2.setVisibility(View.GONE);
                        banner2.setVisibility(View.VISIBLE);
                        banner2.setImageLoader(new GlideImageMidlllunboLoader());
                        banner2.setImages(bannerlist2);
                        banner2.start();
                    }
                    if (bannerlist3.size() == 1){
                        img_banner3.setVisibility(View.VISIBLE);
                        banner3.setVisibility(View.GONE);
                        //计算图片左右间距之和
                        int padding3 = 15;
                        int spacePx3 = (int) (UIUtil.dp2px(mcontext, padding3) * 2);
                        //计算图片宽度
                        int imageWidth3 = UIUtil.getScreenWidth(mcontext) - spacePx3;
                        //计算宽高比，注意数字后面要加上f表示浮点型数字
                        float scale3 = 23f / 8f;
                        //根据图片宽度和比例计算图片高度
                        int imageHeight3 = (int) (imageWidth3 / scale3);
                        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams( imageWidth3,imageHeight3);
                        //设置左右边距
                        params3.leftMargin = (int) UIUtil.dp2px(mcontext, padding3);
                        params3.rightMargin = (int) UIUtil.dp2px(mcontext, padding3);
                        img_banner3.setLayoutParams(params3);
                        GlideUtils.loadImage(mcontext,bannerlist3.get(0),img_banner3);
                    }else {
                        img_banner3.setVisibility(View.GONE);
                        banner3.setVisibility(View.VISIBLE);
                        banner3.setImageLoader(new GlideImageMidlllunboLoader());
                        banner3.setImages(bannerlist3);
                        banner3.start();
                    }*/


                    break;
                case 3:


                    break;
                case 4:

                    break;
                case 11:
                    if (customProgressDialog != null) {
                        if (customProgressDialog.isShowing()) {
                            customProgressDialog.dismiss();
                        }
                    }
                    sharedPreferences.edit().putString("user_id", "").commit();
                    sharedPreferences.edit().putString("username", "").commit();
                    sharedPreferences.edit().putString("nickname", "").commit();
                    sharedPreferences.edit().putString("mobile", "").commit();
                    sharedPreferences.edit().putString("avatar", "").commit();
                    sharedPreferences.edit().putString("score", "").commit();
                    sharedPreferences.edit().putString("student_id", "").commit();
                    sharedPreferences.edit().putString("is_bind_wx", "").commit();
                    sharedPreferences.edit().putString("token", "").commit();
                    break;
                case 12:
                    if (customProgressDialog != null) {
                        if (customProgressDialog.isShowing()) {
                            customProgressDialog.dismiss();
                        }
                    }
                    break;
            }
        }
    };

    private void setflagtwo(int tag) {
        xcyingxiangtwo.removeAllViews();
        int sizetwo = iosmmentallist.get(tag).getCate_list().size();
        for (int n = 0; n < sizetwo; n++) {
            TextView tvview = (TextView) mInflater.inflate(
                    R.layout.item_itemfla2, xcyingxiangtwo, false);
            tvview.setTag(n);
            tvview.setText(iosmmentallist.get(tag).getCate_list().get(n).getName());
            List<HomeListBean.DataBean.IosMentalBean.CateListBean.CourseListBean> course_list = iosmmentallist.get(tag).getCate_list().get(n).getCourse_list();

            if (n == 0) {
                tvview.setTextColor(getResources().getColor(R.color.colormain));
                setTrainData2(course_list);
            } else {
                tvview.setTextColor(getResources().getColor(R.color.color666));
            }
            xcyingxiangtwo.addView(tvview);
            xcyingxiangtwo.getChildAt(n).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TextView tview = (TextView) view;
                    int checktag = (int) tview.getTag();
                    tview.setTextColor(getResources().getColor(R.color.colormain));
                    int childCount = xcyingxiangtwo.getChildCount();
                    for (int m = 0; m < childCount; m++) {
                        TextView childAt = (TextView) xcyingxiangtwo.getChildAt(m);
                        int tag1 = (int) childAt.getTag();
                        if (checktag != tag1) {
                            childAt.setTextColor(getResources().getColor(R.color.color666));
                        }
                    }
                    List<HomeListBean.DataBean.IosMentalBean.CateListBean.CourseListBean> course_list = iosmmentallist.get(tag).getCate_list().get(checktag).getCourse_list();
                    setTrainData2(course_list);

                }
            });
        }
    }

    public void setRecommendData(int position) {
        ll_recommend.removeAllViews();
        if (recommendlist.size() > 0) {
            List<HomeListBean.DataBean.RecommendBean.ListBeanX> list = recommendlist.get(position).getList();
            int size = list.size();
            for (int i = 0; i < size; i++) {
                View view = View.inflate(mcontext, R.layout.item_recoommend, null);
                ImageView img_img = view.findViewById(R.id.img_img);
                TextView tv_name = view.findViewById(R.id.tv_name);
                TextView tv_content = view.findViewById(R.id.tv_content);
                TextView tv_seedetail = view.findViewById(R.id.tv_seedetail);
                TextView tv_freetry = view.findViewById(R.id.tv_freetry);

                String logo_image = list.get(i).getLogo_image();
                if (!"".equals(logo_image)) {
                    GlideUtils.loadRoundCircleImage(mcontext, logo_image, img_img);
                }
                tv_name.setText(list.get(i).getName());
                tv_content.setText(list.get(i).getDescription());
                tv_seedetail.setTag(i);
                String testId = list.get(i).getTest_id();
                if ("0".equals(testId)) {
                    tv_freetry.setVisibility(View.INVISIBLE);
                } else {
                    tv_freetry.setVisibility(View.VISIBLE);
                    tv_freetry.setTag(i);
                }

                tv_freetry.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        token = sharedPreferences.getString("token", "");
                        if ("".equals(token)) {
                            Intent intent = new Intent(mcontext, LoginActivity.class);
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(mcontext, AnswerQueActivity.class);
                            int item = (int) view.getTag();
                            String course_id = list.get(item).getId()+"";
                            String catelog_id = list.get(item).getTest_id()+"";
                            intent.putExtra("catelog_id", catelog_id + "");
                            intent.putExtra("course_id", course_id + "");

                            startActivity(intent);
                        }
                    }
                });

                tv_seedetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int tag = (int) view.getTag();
                        int id = list.get(tag).getId();

                        String is_buy = list.get(tag).getIs_buy();
                        String is_vip = sharedPreferences.getString("is_vip", "");
                        if ("1".equals(is_vip)){
                            //是会员
                            Intent intent = new Intent(mcontext, MyCourseActivity.class);
                            intent.putExtra("id", id + "");
                            startActivity(intent);
                        }else {
                            //不是会员
                            if ("1".equals(is_buy)){
                                //已购买
                                Intent intent = new Intent(mcontext, MyCourseActivity.class);
                                intent.putExtra("id", id + "");
                                startActivity(intent);
                            }else if ("0".equals(is_buy)) {

                                Intent intent = new Intent(mcontext, WebviewActivity.class);
                                intent.putExtra("url", myapp.getSiteurlweb() + "/#/kcxiangqing?id=" + id + "&token=" + token);
                                intent.putExtra("id", id + "");
                                startActivity(intent);
                            }}
                    }
                });

                ll_recommend.addView(view);
            }
        }
    }

    public void setTrainData(int position) {
        ll_train.removeAllViews();
        if (mmentallist.size() > 0) {
            List<HomeListBean.DataBean.MentalBean.ListBean> list = mmentallist.get(position).getList();
//            List<MentalBean.list> list = mmentallist.get(position).getList();
            int size = list.size();
            for (int i = 0; i < size; i++) {
                View view = View.inflate(mcontext, R.layout.item_recoommend, null);
                ImageView img_img = view.findViewById(R.id.img_img);
                TextView tv_name = view.findViewById(R.id.tv_name);
                TextView tv_content = view.findViewById(R.id.tv_content);
                TextView tv_seedetail = view.findViewById(R.id.tv_seedetail);
                TextView tv_freetry = view.findViewById(R.id.tv_freetry);
                String logo_image = list.get(i).getLogo_image();
                if (!"".equals(logo_image)) {
                    GlideUtils.loadRoundCircleImage(mcontext, logo_image, img_img);
                }
                tv_name.setText(list.get(i).getName());
                tv_content.setText(list.get(i).getDescription());

                tv_seedetail.setTag(i);
                String testId = list.get(i).getTest_id();
                if ("0".equals(testId)) {
                    tv_freetry.setVisibility(View.INVISIBLE);
                } else {
                    tv_freetry.setVisibility(View.VISIBLE);
                    tv_freetry.setTag(i);
                }

                tv_freetry.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        token = sharedPreferences.getString("token", "");
                        if ("".equals(token)) {
                            Intent intent = new Intent(mcontext, LoginActivity.class);
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(mcontext, AnswerQueActivity.class);
                            int item = (int) view.getTag();
                            String course_id = list.get(item).getId()+"";
                            String catelog_id = list.get(item).getTest_id();
                            intent.putExtra("catelog_id", catelog_id + "");
                            intent.putExtra("course_id", course_id + "");

                            startActivity(intent);
                        }
                    }
                });
                tv_seedetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int tag = (int) view.getTag();
                        int id = list.get(tag).getId();
                        String is_buy = list.get(tag).getIs_buy();
                        String is_vip = sharedPreferences.getString("is_vip", "");
                        if ("1".equals(is_vip)) {
                            //是会员
                            Intent intent = new Intent(mcontext, MyCourseActivity.class);
                            intent.putExtra("id", id + "");
                            startActivity(intent);
                        } else {
                            //不是会员
                            if ("1".equals(is_buy)) {
                                //已购买
                                Intent intent = new Intent(mcontext, MyCourseActivity.class);
                                intent.putExtra("id", id + "");
                                startActivity(intent);
                            } else if ("0".equals(is_buy)) {
                                Intent intent = new Intent(mcontext, WebviewActivity.class);
                                intent.putExtra("url", myapp.getSiteurlweb() + "/#/kcxiangqing?id=" + id + "&token=" + token);
                                intent.putExtra("id", id + "");
                                startActivity(intent);
                            }
                        }
                    }
                });

                ll_train.addView(view);
            }
        }
    }

    public void setTrainData2(List<HomeListBean.DataBean.IosMentalBean.CateListBean.CourseListBean> course_list) {
//        List<HomeListBean.DataBean.IosMentalBean.CateListBean.CourseListBean> course_list = iosmmentallist.get(tag).getCate_list().get(n).getCourse_list();

        MyLog.e("数量数量", course_list.size() + "   数量");
        ll_train.removeAllViews();
        if (course_list.size() > 0) {
            int size = course_list.size();
            for (int i = 0; i < size; i++) {
                View view = View.inflate(mcontext, R.layout.item_recoommend, null);
                ImageView img_img = view.findViewById(R.id.img_img);
                TextView tv_name = view.findViewById(R.id.tv_name);
                TextView tv_content = view.findViewById(R.id.tv_content);
                TextView tv_seedetail = view.findViewById(R.id.tv_seedetail);
                TextView tv_freetry = view.findViewById(R.id.tv_freetry);
                String logo_image = course_list.get(i).getLogo_image();
                if (!"".equals(logo_image)) {
                    GlideUtils.loadRoundCircleImage(mcontext, logo_image, img_img);
                }
                tv_name.setText(course_list.get(i).getName());
                tv_content.setText(course_list.get(i).getDescription());

                tv_seedetail.setTag(i);
                String testId = course_list.get(i).getTest_id();
                if ("0".equals(testId)) {
                    tv_freetry.setVisibility(View.INVISIBLE);
                } else {
                    tv_freetry.setVisibility(View.VISIBLE);
                    tv_freetry.setTag(i);
                }

                tv_freetry.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        token = sharedPreferences.getString("token", "");
                        if ("".equals(token)) {
                            Intent intent = new Intent(mcontext, LoginActivity.class);
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(mcontext, AnswerQueActivity.class);
                            int item = (int) view.getTag();
                            String course_id = course_list.get(item).getId()+"";
                            String catelog_id = course_list.get(item).getTest_id();
                            intent.putExtra("catelog_id", catelog_id + "");
                            intent.putExtra("course_id", course_id + "");


                            startActivity(intent);
                        }
                    }
                });
                tv_seedetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int tag = (int) view.getTag();
                        int id = course_list.get(tag).getId();
                        String is_buy = course_list.get(tag).getIs_buy();
                        String is_vip = sharedPreferences.getString("is_vip", "");
                        if ("1".equals(is_vip)) {
                            //是会员
                            Intent intent = new Intent(mcontext, MyCourseActivity.class);
                            intent.putExtra("id", id + "");
                            startActivity(intent);
                        } else {
                            //不是会员
                            if ("1".equals(is_buy)) {
                                //已购买
                                Intent intent = new Intent(mcontext, MyCourseActivity.class);
                                intent.putExtra("id", id + "");
                                startActivity(intent);
                            } else if ("0".equals(is_buy)) {
                                //未购买
                                Intent intent = new Intent(mcontext, WebviewActivity.class);
                                intent.putExtra("url", myapp.getSiteurlweb() + "/#/kcxiangqing?id=" + id + "&token=" + token);
                                intent.putExtra("id", id + "");
                                startActivity(intent);
                            }
                        }

                    }
                });

                ll_train.addView(view);
            }
        }
    }

    public void setFreerepeed() {
        int size = freeListlist.size();
        if (size > 0) {
            if (size == 1) {
                ll_freespeedone.setVisibility(View.VISIBLE);
                ll_freespeedtwo.setVisibility(View.INVISIBLE);
                ll_freespeedthree.setVisibility(View.INVISIBLE);
                String image = freeListlist.get(0).getImage();
                if (!"".equals(image)) {
                    GlideUtils.loadRoundCircleImage(mcontext, image, img_freespeedone);
                }
                int free_switch = freeListlist.get(0).getFree_switch();
                if ("0".equals(free_switch + "")) {
                    //付费\
                    tv_freeone_flag.setText("收费");
                    tv_freeone_flag.setVisibility(View.VISIBLE);
                    tv_freeone_flag.setBackgroundResource(R.drawable.buy_flag_bg);
                } else if ("1".equals(free_switch + "")) {
                    //免费
                    tv_freeone_flag.setText("免费");
                    tv_freeone_flag.setVisibility(View.VISIBLE);
                    tv_freeone_flag.setBackgroundResource(R.drawable.free_flag_bg);
                } else {
                    tv_freeone_flag.setVisibility(View.GONE);
                }

                tv_freespeedone.setText(freeListlist.get(0).getName());
                tv_authorone.setText(freeListlist.get(0).getAuthor());
            } else if (size == 2) {
                ll_freespeedone.setVisibility(View.VISIBLE);
                ll_freespeedtwo.setVisibility(View.VISIBLE);
                ll_freespeedthree.setVisibility(View.INVISIBLE);
                String image = freeListlist.get(0).getImage();
                if (!"".equals(image)) {
                    GlideUtils.loadRoundCircleImage(mcontext, image, img_freespeedone);
                }
                tv_freespeedone.setText(freeListlist.get(0).getName());
                tv_authorone.setText(freeListlist.get(0).getAuthor());

                String image2 = freeListlist.get(1).getImage();
                if (!"".equals(image2)) {
                    GlideUtils.loadRoundCircleImage(mcontext, image2, img_freespeedtwo);
                }
                tv_freespeedtwo.setText(freeListlist.get(1).getName());
                tv_authortwo.setText(freeListlist.get(1).getAuthor());
                int free_switch = freeListlist.get(0).getFree_switch();
                if ("0".equals(free_switch + "")) {
                    //付费\
                    tv_freeone_flag.setText("收费");
                    tv_freeone_flag.setVisibility(View.VISIBLE);
                    tv_freeone_flag.setBackgroundResource(R.drawable.buy_flag_bg);
                } else if ("1".equals(free_switch + "")) {
                    //免费
                    tv_freeone_flag.setText("免费");
                    tv_freeone_flag.setVisibility(View.VISIBLE);
                    tv_freeone_flag.setBackgroundResource(R.drawable.free_flag_bg);
                } else {
                    tv_freeone_flag.setVisibility(View.GONE);
                }
                int free_switch2 = freeListlist.get(1).getFree_switch();
                if ("0".equals(free_switch2 + "")) {
                    //付费
                    tv_freetwo_flag.setText("收费");
                    tv_freetwo_flag.setVisibility(View.VISIBLE);
                    tv_freetwo_flag.setBackgroundResource(R.drawable.buy_flag_bg);
                } else if ("1".equals(free_switch2 + "")) {
                    //免费
                    tv_freetwo_flag.setText("免费");
                    tv_freetwo_flag.setVisibility(View.VISIBLE);
                    tv_freetwo_flag.setBackgroundResource(R.drawable.free_flag_bg);
                } else {
                    tv_freetwo_flag.setVisibility(View.GONE);
                }

            } else if (size == 3) {
                ll_freespeedone.setVisibility(View.VISIBLE);
                ll_freespeedtwo.setVisibility(View.VISIBLE);
                ll_freespeedthree.setVisibility(View.VISIBLE);
                String image = freeListlist.get(0).getImage();
                if (!"".equals(image)) {
                    GlideUtils.loadRoundCircleImage(mcontext, image, img_freespeedone);
                }
                tv_freespeedone.setText(freeListlist.get(0).getName());
                tv_authorone.setText(freeListlist.get(0).getAuthor());

                String image2 = freeListlist.get(1).getImage();
                if (!"".equals(image2)) {
                    GlideUtils.loadRoundCircleImage(mcontext, image2, img_freespeedtwo);
                }
                tv_freespeedtwo.setText(freeListlist.get(1).getName());
                tv_authortwo.setText(freeListlist.get(1).getAuthor());

                String image3 = freeListlist.get(2).getImage();
                if (!"".equals(image3)) {
                    GlideUtils.loadRoundCircleImage(mcontext, image3, img_freespeedthree);
                }
                tv_freespeedthree.setText(freeListlist.get(2).getName());
                tv_authorthree.setText(freeListlist.get(2).getAuthor());

                int free_switch = freeListlist.get(0).getFree_switch();
                if ("0".equals(free_switch + "")) {
                    //付费\
                    tv_freeone_flag.setText("收费");
                    tv_freeone_flag.setVisibility(View.VISIBLE);
                    tv_freeone_flag.setBackgroundResource(R.drawable.buy_flag_bg);
                } else if ("1".equals(free_switch + "")) {
                    //免费
                    tv_freeone_flag.setText("免费");
                    tv_freeone_flag.setVisibility(View.VISIBLE);
                    tv_freeone_flag.setBackgroundResource(R.drawable.free_flag_bg);
                } else {
                    tv_freeone_flag.setVisibility(View.GONE);
                }
                int free_switch2 = freeListlist.get(1).getFree_switch();
                if ("0".equals(free_switch2 + "")) {
                    //付费
                    tv_freetwo_flag.setText("收费");
                    tv_freetwo_flag.setVisibility(View.VISIBLE);
                    tv_freetwo_flag.setBackgroundResource(R.drawable.buy_flag_bg);
                } else if ("1".equals(free_switch2 + "")) {
                    //免费
                    tv_freetwo_flag.setText("免费");
                    tv_freetwo_flag.setVisibility(View.VISIBLE);
                    tv_freetwo_flag.setBackgroundResource(R.drawable.free_flag_bg);
                } else {
                    tv_freetwo_flag.setVisibility(View.GONE);
                }
                int free_switch3 = freeListlist.get(2).getFree_switch();
                if ("0".equals(free_switch3 + "")) {
                    //付费
                    tv_freethree_flag.setText("收费");
                    tv_freethree_flag.setVisibility(View.VISIBLE);
                    tv_freethree_flag.setBackgroundResource(R.drawable.buy_flag_bg);
                } else if ("1".equals(free_switch3 + "")) {
                    //免费
                    tv_freethree_flag.setText("免费");
                    tv_freethree_flag.setVisibility(View.VISIBLE);
                    tv_freethree_flag.setBackgroundResource(R.drawable.free_flag_bg);
                } else {
                    tv_freethree_flag.setVisibility(View.GONE);
                }

            }
        }
    }

    public class GlideImageMidlllunboLoader extends ImageLoader {

        @Override
        public ImageView createImageView(Context context) {


            XmlPullParser parser = getResources().getXml(R.xml.layout_ratioimageview);
            AttributeSet attributes = Xml.asAttributeSet(parser);

            ImageView imageView = new RatioImageView(context, attributes);
            imageView.setAdjustViewBounds(true);

            //计算图片左右间距之和
            int padding3 = 15;
            int spacePx3 = (int) (UIUtil.dp2px(context, padding3) * 2);
            //计算图片宽度
            int imageWidth3 = UIUtil.getScreenWidth(context) - spacePx3;
            //计算宽高比，注意数字后面要加上f表示浮点型数字
            float scale3 = 23f / 8f;
            //根据图片宽度和比例计算图片高度
            int imageHeight3 = (int) (imageWidth3 / scale3);
            LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(imageWidth3, imageHeight3);
            //设置左右边距
            params3.leftMargin = (int) UIUtil.dp2px(context, padding3);
            params3.rightMargin = (int) UIUtil.dp2px(context, padding3);
            banner3.setLayoutParams(params3);

            /*//计算图片左右间距之和
            int padding2 = 15;
            int spacePx2 = (int) (UIUtil.dp2px(context, padding2) * 2);
            //计算图片宽度
            int imageWidth2 = UIUtil.getScreenWidth(context) - spacePx2;
            //计算宽高比，注意数字后面要加上f表示浮点型数字
            float scale2 = 23f / 10f;
            //根据图片宽度和比例计算图片高度
            int imageHeight2 = (int) (imageWidth2 / scale2);
            LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(imageWidth2, imageHeight2);
            //设置左右边距
            params2.leftMargin = (int) UIUtil.dp2px(context, padding2);
            params2.rightMargin = (int) UIUtil.dp2px(context, padding2);
            banner2.setLayoutParams(params2);*/


            //计算图片左右间距之和
            int padding1 = 15;
            int spacePx1 = (int) (UIUtil.dp2px(context, padding1) * 2);
            //计算图片宽度
            int imageWidth1 = UIUtil.getScreenWidth(context) - spacePx1;
            //计算宽高比，注意数字后面要加上f表示浮点型数字
            float scale1 = 23f / 10f;
            //根据图片宽度和比例计算图片高度
            int imageHeight1 = (int) (imageWidth1 / scale1);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(imageWidth1, imageHeight1);
            //设置左右边距
            params1.leftMargin = (int) UIUtil.dp2px(context, padding1);
            params1.rightMargin = (int) UIUtil.dp2px(context, padding1);
            banner.setLayoutParams(params1);



            return imageView;
        }

        @Override
        public void displayImage(final Context context, final Object path, ImageView imageView) {
//            Glide.with(getApplicationContext()).load(path).transform(new FitCenter(context), new GlideRoundTransform(context)).into(imageView);
            if ("".equals(path)) {
                imageView.setImageResource(R.mipmap.ic_launcher);
            } else {
//                GlideUtils.loadRoundCircleImage(context, path + "", imageView);
                GlideUtils.loadImage(context, path + "", imageView);
            }
        }
    }

    private boolean islogin = false;

    /**
     * 获取屏幕宽度
     */
    private void getScreenDen() {
//        dm = new DisplayMetrics();
//        heightPixels = dm.heightPixels;
//        getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager manager = getActivity().getWindowManager();
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        //以要素为单位
        width = dm.widthPixels;
        int heightPixels = dm.heightPixels;
        MyLog.e("获取屏幕高度", heightPixels + "    高度   " + width);
    }


    @Override
    public void onResume() {
        super.onResume();
        token = sharedPreferences.getString("token", "");
        getData();
        MyLog.e("首页", "onResume");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        MyLog.e("首页", "onHiddenChanged     " + hidden);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
